package org.example;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class Foods {
    private ArrayList<Point> foods;
    private int gridWidth;
    private int gridHeight;
    private int tileSize;
    private Random random = new Random();
    private int maxFoodCount = 100; // 食物最大数量上限


    public Foods(int gridWidth, int gridHeight, int tileSize) {
        this.gridWidth = gridWidth;
        this.gridHeight = gridHeight;
        this.tileSize = tileSize;
        foods = new ArrayList<>();
    }
    // 生成食物
    int foodCount = 0;//0个食物
    public void generateFood(Snake snake, Buildings buildings, String mapType) {
        if (foods.size() >= maxFoodCount) {
            return;
        }
        foodCount = 1 + random.nextInt(3);

        for (int i = 0; i < foodCount; i++) {
            Point newFood;
            boolean validPosition;

            do {
                validPosition = true;
                int gridX = random.nextInt(gridWidth);
                int gridY = random.nextInt(gridHeight);
                if (random.nextInt(10) == 0){
                    newFood = new Point(gridX, gridY, new Color(255, 215, 0)); // 金色
                }
                else if (random.nextInt(10) == 1) {
                    newFood = new Point(gridX, gridY, new Color(128, 0, 128)); // 绿色(有毒)
                }
                else {
                    newFood = new Point(gridX, gridY, new Color(220, 60, 60));
                }


                // 检查是否与蛇重叠
                for (Point segment : snake.getBody()) {
                    if (segment.equals(newFood)) {
                        validPosition = false;
                        break;
                    }
                }

                // 检查是否与障碍物重叠
                if (buildings != null) {
                    for (Point building : buildings.getBuildings()) {
                        if (building.equals(newFood)) {
                            validPosition = false;
                            break;
                        }
                    }
                }

                // 检查是否与其他食物重叠
                for (Point food : foods) {
                    if (food.equals(newFood)) {
                        validPosition = false;
                        break;
                    }
                }

                // 对于有边界地图，确保食物不在边界上
                if (mapType.equals("有边界")) {
                    if (gridX == 0 || gridX == gridWidth - 1 ||
                            gridY == 0 || gridY == gridHeight - 1) {
                        validPosition = false;
                    }
                }

            } while (!validPosition);

            foods.add(newFood);
        }
    }
    public void generateFood(Snake snake,Snake snake2, Buildings buildings, String mapType) {
        if (foods.size() >= maxFoodCount) {
            return;
        }
        foodCount = 1 + random.nextInt(3);
        for (int i = 0; i < foodCount; i++) {
            Point newFood;
            boolean validPosition;

            do {
                validPosition = true;
                int gridX = random.nextInt(gridWidth);
                int gridY = random.nextInt(gridHeight);
                if (random.nextInt(5) == 0){
                    newFood = new Point(gridX, gridY, new Color(255, 215, 0)); // 金色
                }
                else {
                    newFood = new Point(gridX, gridY, new Color(220, 60, 60));
                }


                // 检查是否与蛇重叠
                for (Point segment : snake.getBody()) {
                    if (segment.equals(newFood)) {
                        validPosition = false;
                        break;
                    }
                }
                for (Point segment : snake2.getBody()) {
                    if (segment.equals(newFood)) {
                        validPosition = false;
                        break;
                    }
                }

                // 检查是否与障碍物重叠
                if (buildings != null) {
                    for (Point building : buildings.getBuildings()) {
                        if (building.equals(newFood)) {
                            validPosition = false;
                            break;
                        }
                    }
                }

                // 检查是否与其他食物重叠
                for (Point food : foods) {
                    if (food.equals(newFood)) {
                        validPosition = false;
                        break;
                    }
                }

                // 对于有边界地图，确保食物不在边界上
                if (mapType.equals("有边界")) {
                    if (gridX == 0 || gridX == gridWidth - 1 ||
                            gridY == 0 || gridY == gridHeight - 1) {
                        validPosition = false;
                    }
                }

            } while (!validPosition);

            foods.add(newFood);
        }
    }
    //添加食物
    public void addFood(int gridX, int gridY, Color color, Buildings buildings) {
        Point newFood = new Point(gridX, gridY, color);
        boolean validPosition = true;;
        // 检查是否与障碍物重叠
        if (buildings != null) {
            for (Point building : buildings.getBuildings()) {
                if (building.equals(newFood)) {
                    validPosition = false;
                    break;
                }
            }
        }
        //检查是否超出地图
        if (gridX < 0 || gridX >= gridWidth || gridY < 0 || gridY >= gridHeight) {
            validPosition = false;
        }
        // 检查是否与其他食物重叠
        for (Point food : foods) {
            if (food.equals(newFood)) {
                validPosition = false;
                break;
            }
        }
        if (validPosition)
            foods.add(newFood);
    }
    public Point checkCollision(Point point) {
        for (int i = 0; i < foods.size(); i++) {
            Point food = foods.get(i);
            if (food.equals(point)) {
                // 移除被吃掉的食物
                foods.remove(i);
                return food;
            }
        }
        return null;
    }

    public void draw(Graphics2D g) {
        for (Point food : foods) {
            g.setColor(food.getColor());

            int pixelX = food.getGridX() * tileSize;
            int pixelY = food.getGridY() * tileSize;

            if (food.getColor().equals(new Color(255, 215, 0))) {
                // 金色食物
                g.fillOval(pixelX, pixelY, tileSize, tileSize);
                g.setColor(Color.WHITE);
                g.fillOval(pixelX + tileSize/4, pixelY + tileSize/4, tileSize/10, tileSize/10);
            } else if (food.getColor().equals(new Color(128, 0, 128))) {
                // 有毒食物
                g.fillOval(pixelX, pixelY, tileSize, tileSize);
                g.setColor(Color.WHITE);
                g.fillOval(pixelX + tileSize/4, pixelY + tileSize/4, tileSize/10, tileSize/10);
            } else {
                // 普通食物
                g.fillOval(pixelX, pixelY, tileSize, tileSize);
                g.setColor(Color.WHITE);
                g.fillOval(pixelX + tileSize/4, pixelY + tileSize/4, tileSize/10, tileSize/10);
            }
        }
    }

    public ArrayList<Point> getFoods() {
        return foods;
    }

    public void setFoods(ArrayList<Point> foods) {
        this.foods = foods;
    }
}